package com.wjgl.service;

import java.util.List;

import com.wjgl.dao.RoleDAO;
import com.wjgl.domain.Role;

public class RoleService {
	private RoleDAO roleDAO;

	public void setRoleDAO(RoleDAO roleDAO) {
		this.roleDAO = roleDAO;
	}
	
	public List<Role> getAllRoles(){
		return (List<Role>)roleDAO.findAll();
	}
	
	public Role findRoleById(int id){
		return roleDAO.findById(id);
	}
	
	public void updateRole(Role role){
		roleDAO.update(role);
	}
	
	public boolean deleteRole(int id){
		boolean b = false;
		Role role = this.findRoleById(id);
		if(role != null){
			roleDAO.delete(role);
			b=true;
		}
		return b;
		
	}
	
	public boolean addRole(Role role){
		boolean b = true;
		
		List list = roleDAO.findByProperty("name", role.getName());
		if(list.size() == 0)
			roleDAO.save(role);
		else
			b=false;
		return b;
		
	}
}
